package com.hk.yajin.base;


import android.Manifest;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.hk.developmentlib.utills.NetworkInfoUtils;
import com.hk.developmentlib.utills.UiUtil;
import com.hk.yajin.R;
import com.hk.yajin.view.DefaultView;


/**
 * Fragment基类
 *
 * @author wl
 */

public abstract class BaseFragment extends Fragment implements View.OnClickListener, DefaultView.DefaultViewClickListener {
    /*
    * 1.读、写权限
    * 2.相机
    * 3.联系人读写，获取账号GET_ACCOUNTS
    * 4.获取定位
    *5.监听电话状态，打电话
    *6.录音
    *7.短信组
    * */
    protected String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.CAMERA,
            Manifest.permission.READ_CONTACTS,
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.READ_PHONE_STATE,
            Manifest.permission.RECORD_AUDIO,
            Manifest.permission.READ_SMS,
    };
    private static final String TAG = "BaseActivity";

    protected Resources res;

    protected Context mContext;


    protected Dialog dialog;

    protected boolean isCreated = false;

    protected ImageView iv_loading;
    public static final String URL = "url";
    public static final String VIDEO = "video";
    public static final String DATA_STRING = "data";
    /**********/
    protected RelativeLayout rootView;
    protected View mContentView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.res = this.getResources();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreateView(inflater, container, savedInstanceState);
        if (mContentView == null) {
            rootView = new RelativeLayout(getActivity());
            mContentView = initView(inflater, container);
            rootView.addView(mContentView, getLayoutParams());
        } else {
            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (parent != null) {
                parent.removeView(rootView);
            }
        }

        return rootView;
    }

    /**
     * @return 初始化布局
     */
    protected abstract View initView(LayoutInflater inflater, ViewGroup container);


    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mContext = getActivity();
    }


    public void hideDialog() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }


    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
    }


    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroy() {
        UiUtil.getHandler().removeCallbacksAndMessages(null);
        super.onDestroy();

    }

    @Override
    public void onDestroyView() {
        // TODO Auto-generated method stub
        super.onDestroyView();
    }

    public void initialization() {
    }


    /**
     * 创建旋转动画
     *
     * @param context
     * @return
     */
    private static Animation setAnimation(Context context) {
        Animation mAnimation = AnimationUtils.loadAnimation(context, R.anim.loading_tip);// 设置旋转效果
        mAnimation.setInterpolator(new LinearInterpolator());// LinearInterpolator为匀速效果
        return mAnimation;
    }


    public void bindEvent() {
    }


    protected void showShortToast(int pResId) {
        showShortToast(getString(pResId));
    }

    protected void showLongToast(String pMsg) {
        Toast.makeText(mContext, pMsg, Toast.LENGTH_LONG).show();
    }

    protected void showShortToast(String pMsg) {
        Toast.makeText(mContext, pMsg, Toast.LENGTH_SHORT).show();
    }

    protected void openActivity(Class<?> pClass) {
        openActivity(pClass, null);
    }

    protected void openActivity(Class<?> pClass, Bundle pBundle) {
        Intent intent = new Intent(mContext, pClass);
        if (pBundle != null) {
            intent.putExtras(pBundle);
        }
        startActivity(intent);
    }

    protected void openActivityClearTop(Class<?> pClass, Bundle pBundle) {
        Intent intent = new Intent(mContext, pClass);
        if (pBundle != null) {
            intent.putExtras(pBundle);
        }
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
    }

    protected void openActivity(String pAction) {
        openActivity(pAction, null);
    }

    protected void openActivity(String pAction, Bundle pBundle) {
        Intent intent = new Intent(pAction);
        if (pBundle != null) {
            intent.putExtras(pBundle);
        }
        startActivity(intent);
    }

    protected void openActivityForResult(Class<?> pClass, int requestCode) {
        openActivityForResult(pClass, requestCode, null);
    }

    protected void openActivityForResult(Class<?> pClass, int requestCode,
                                         Bundle pBundle) {
        Intent intent = new Intent(getActivity(), pClass);
        if (pBundle != null) {
            intent.putExtras(pBundle);
        }
        startActivityForResult(intent, requestCode);
    }


    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // TODO Auto-generated method stub
        return false;
    }


    @Override
    public void onClick(View v) {

    }

    protected boolean isNetWorkConnect() {
        if (new NetworkInfoUtils(MyApplication.getInstance()).getNetType() == -1) {
            showShortToast(getString(R.string.badnetwork));
            return false;
        } else {
            return true;
        }
    }

    protected void startActivity(Class<?> cls) {
        startActivity(new Intent(getActivity(), cls));

    }


    /*加载框*/

    public void showLoadingViewWhiteBg() {
        BaseActivity baseActivity = getBaseActivity();
        if (baseActivity != null) {
            baseActivity.showLoadingViewWhiteBg();
        }
    }


    public void showLoadingView() {
        BaseActivity baseActivity = getBaseActivity();
        if (baseActivity != null) {
            baseActivity.showLoadingView();
        }
    }


    public void hideLoadingView() {
        BaseActivity baseActivity = getBaseActivity();
        if (baseActivity != null) {
            baseActivity.hideLoadingView();
        }
    }

    public BaseActivity getBaseActivity() {
        Activity activity = getActivity();
        if (activity != null && activity instanceof BaseActivity) {
            return (BaseActivity) activity;
        }
        return null;
    }

    /*********************
     * 缺省页DefaultView相关方法
     ***********************/
    private DefaultView defaultView;


    protected static final int NOT_LOGIN = 1;
    protected static final int NO_NETWORK = 2;
    protected static final int NO_DTTA = 3;
    protected static final int API_ERROR = 4;

    public void showDefaultView_Not_Login() {
        getDefaultViewBuild().setTag(NOT_LOGIN).setHintText(R.string.state_login_out).setBtnText(R.string.state_login_out).show();
    }

    public void showDefaultView_NO_NetWork() {
        getDefaultViewBuild().setTag(NO_NETWORK).setHintText(R.string.badnetwork).setBtnText(R.string.badnetwork).show();
    }

    public void showDefaultView_NO_DTTA() {
        getDefaultViewBuild().setTag(NO_DTTA).setHintText(R.string.nodata).setBtnText(R.string.reload).show();
    }

    public void showDefaultView_API_ERROR() {
        getDefaultViewBuild().setTag(API_ERROR).setHintText(R.string.api_error).setBtnText(R.string.reload).show();
    }


    public DefaultView.DefaultViewBuild getDefaultViewBuild() {
        initDefaultView();
        return defaultView.getBuild();
    }


    public void hideDefaultView() {
        if (defaultView != null) {
            defaultView.hide();
        }
    }

    public void showDefaultView() {
        initDefaultView();
        defaultView.show();
    }

    public boolean defaultViewIsShow() {
        if (defaultView == null) {
            return false;
        } else {
            return defaultView.isShow();
        }
    }

    private void initDefaultView() {
        if (defaultView == null) {
            defaultView = new DefaultView();
            defaultView.setClickListener(this);
            rootView.addView(defaultView.getRootView(), getLayoutParams());
        }
    }


    private RelativeLayout.LayoutParams getLayoutParams() {
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        return lp;
    }

    @Override
    public void defaultViewClick(int tag) {

    }

}